<template>
  <div class="app-container">
    <div class="nav-search">
      <el-date-picker
        v-model="searchParams.createData"
        :clearable="false"
        type="date"
        value-format="yyyy-MM-dd"
        format="yyyy-MM-dd"
        placeholder="创建日期"
      >
      </el-date-picker>
      <el-input v-model="searchParams.searchStr" placeholder="助理姓名/诊所名称"></el-input>
      <span>
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
        <el-button class="filter-item" size="mini" type="warning" icon="el-icon-download" @click="exportList">导出</el-button>
      </span>
      <div v-if="searchInfo.code == 'aid:nowaid'" class="tableHandle">
        <el-button class="filter-item" size="mini" type="primary" @click="resetPassword">重置密码</el-button>
        <el-button class="filter-item" type="danger" size="mini" @click="removeAid">删除助理</el-button>
        <el-button class="filter-item" size="mini" type="success" @click="addAid">新增助理</el-button>
      </div>
    </div>
    <div class="tableList">
      <el-table
        ref="table"
        v-loading="loading"
        stripe
        :data="tableData"
        size="small"
        style="width: 100%;"
        @selection-change="selectionChangeHandler"
      >
        <el-table-column type="selection"></el-table-column>
        <el-table-column prop="assistantName" label="姓名"></el-table-column>
        <el-table-column prop="mobileNum" label="手机号码"></el-table-column>
        <el-table-column prop="clinicName" label="诊所名称"></el-table-column>
        <el-table-column prop="createDate" label="创建时间"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <span>{{ scope.row.stat == 1 ? "启用" : "停用" }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="searchInfo.code == 'aid:nowaid'" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        background
        :page-size.sync="page.size"
        :total="total"
        :current-page.sync="page.page"
        style="margin-top: 8px; text-align: right;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChangeHandler($event)"
        @current-change="pageChangeHandler"
      ></el-pagination>
    </div>
    <div class="dialog">
      <el-dialog
        :visible.sync="dialogVisible"
        width="30%"
        :modal="false"
        top="250px"
      >
        <span slot="title" class="dialog-title">{{ dialogTitle }}</span>
        <div class="dialogBody">
          <el-form label-width="100px" :model="aidform" size="mini">
            <el-form-item label="助理账号">
              <p style="line-height: 0px;">{{ aidform.assistantAccount }}</p>
            </el-form-item>
            <el-form-item label="助理姓名">
              <el-input v-model="aidform.assistantName" :disabled="isAssistantNameDisable" placeholder="请输入助理姓名"></el-input>
            </el-form-item>
            <el-form-item label="移动电话">
              <el-input v-model="aidform.mobileNum" placeholder="请输入移动电话" @input="mobileNumChange(aidform.mobileNum)"></el-input>
            </el-form-item>
            <el-form-item label="当前诊所">
              <p style="line-height: 0px;">{{ aidform.clinicName }}</p>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="aidformClose">取 消</el-button>
          <el-button type="primary" @click="aidformSave">保 存</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { query, add, edit, del, download, queryDoctorInfo, queryClinic, resetPassword } from '@/api/aid';
import { mapGetters } from 'vuex';
function InitParam() {
  return {
    'assistantName': '', // 助理名称
    'clinicId': 'LM1101AA', // 诊所id
    'doctorId': '', // 医生id
    'mobileNum': '', // 移动电话
    'assistantAccount': '', // 助理账号
    'clinicName': '' // 诊所名称
  };
}

export default {
  components: {},
  data() {
    return {
      loading: false,
      dialogVisible: false,
      dialogTitle: '',
      searchParams: {
        searchStr: '', // 助理/诊所名称
        createData: '', // 创建时间
        sort: '',
        stat: '' // 1 启用 0 停用
      },
      page: {
        page: 0,
        size: 10
      },
      tableData: [],
      total: 0,
      aidform: {
        'assistantName': '', // 助理名称
        'clinicId': 'LM1101AA', // 诊所id
        'doctorId': '', // 医生id
        'mobileNum': '', // 移动电话
        'assistantAccount': '', // 助理账号
        'clinicName': '' // 诊所名称
      },
      multipleSelection: [],
      doctorInfo: {}, // 医生详情
      assistantIdList: [],
      clinicInfo: {}, // 诊所信息
      isAssistantNameDisable: false
    };
  },
  computed: {
    ...mapGetters(['searchInfo', 'user'])
  },
  watch: {
    searchInfo(newVal, oldVal) {
      console.log('new------', newVal);
      if (newVal.code === 'aid:nowaid') {
        this.searchParams.stat = 1;
        this.toQuery();
      } else if (newVal.code === 'aid:alllist') {
        this.searchParams.stat = '';
        this.toQuery();
      }
    }
  },
  mounted() {
    // 默认当前助理
    this.getAidList({ page: 0, size: 10, stat: 1 });
    this.searchInfo.code = 'aid:nowaid';
    this.queryDoctorDetail();
    this.getClinic();
  },
  methods: {
    // 全选
    selectionChangeHandler(val) {
      this.assistantIdList = [];
      this.multipleSelection = val;
      this.multipleSelection.forEach(item => {
        this.assistantIdList.push(item.assistantId);
      });
      console.log('multipleSelection-----', this.multipleSelection);
    },
    // 获取助理列表
    getAidList(params) {
      this.loading = true;
      query(params)
        .then(res => {
          this.tableData = res.content || [];
          this.total = res.totalElements || 0;
          this.loading = false;
        })
        .catch(er => {
          this.loading = false;
        });
    },
    // 搜索
    toQuery() {
      const search = {
        page: this.page.page - 1,
        size: this.page.size,
        ...this.searchParams
      };
      this.getAidList(search);
    },
    // 导出
    exportList() {
      const downloadObj = {
        createData: '',
        idList: this.assistantIdList || [],
        searchStr: '',
        stat: ''
      };
      download(downloadObj).then(res => {
        const url = window.URL.createObjectURL(res);
        var a = document.createElement('a');
        document.body.appendChild(a);
        a.href = url;
        a.download = '助理数据.xlsx';
        a.click();
        window.URL.revokeObjectURL(url);
      }).catch(er => {});
    },
    // 新增助理
    addAid() {
      this.dialogVisible = true;
      this.dialogTitle = '新增助理';
      this.isAssistantNameDisable = false;
      this.aidform.doctorId = this.doctorInfo.id;
      this.aidform.clinicName = this.clinicInfo.clinicName; // 诊所赋值
      this.aidform.assistantAccount = '@' + this.doctorInfo.id; // 助理账号后缀赋值
    },
    // 编辑
    handleEdit(index, row) {
      this.dialogVisible = true;
      this.dialogTitle = '编辑助理';
      this.isAssistantNameDisable = true;
      this.aidform.assistantName = row.assistantName;
      this.aidform.mobileNum = row.mobileNum;
      this.aidform.clinicId = row.clinicId;
      this.aidform.doctorId = row.doctorId;
      this.aidform.assistantId = row.assistantId;
      this.aidform.stat = row.stat;
      this.aidform.clinicName = row.clinicName;
      this.aidform.assistantAccount = row.mobileNum + '@' + row.doctorId;
    },
    // 行内删除
    handleDelete(index, row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        del([row.assistantId]).then(res => {
          this.$notify({
            title: '提示',
            message: '删除成功',
            type: 'success',
            duration: 5000
          });
          this.toQuery();
        });
      });
    },
    // 重置密码
    resetPassword() {
      if (this.multipleSelection.length === 0) {
        this.$notify({
          title: '提示',
          message: '请选择需要重置的助理',
          type: 'warning',
          duration: 5000
        });
      } else {
        this.$confirm('此操作将重置密码, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          resetPassword(this.assistantIdList).then(res => {
            this.$notify({
              title: '提示',
              message: '重置成功',
              type: 'success',
              duration: 5000
            });
            this.toQuery();
          });
        });
      }
    },
    // 删除助理按钮
    removeAid() {
      if (this.assistantIdList.length === 0) {
        this.$notify({
          title: '提示',
          message: '请选择需要删除的助理',
          type: 'warning',
          duration: 5000
        });
      } else {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          del(this.assistantIdList).then(res => {
            this.$notify({
              title: '提示',
              message: '删除成功',
              type: 'success',
              duration: 5000
            });
            this.toQuery();
          });
        });
      }
    },
    sizeChangeHandler() {
      this.toQuery();
    },
    // 页数变更
    pageChangeHandler(pageIndex) {
      this.toQuery();
    },
    // 新增&编辑助理保存
    aidformSave() {
      let fun = null;
      let text = '';
      const param = JSON.parse(JSON.stringify(this.aidform));
      if (this.dialogTitle === '新增助理') {
        fun = add;
        text = '新增成功';
        delete param.assistantAccount;
        delete param.clinicName;
      } else {
        fun = edit;
        text = '编辑成功';
        delete param.clinicId;
        delete param.clinicName;
        delete param.doctorId;
        delete param.assistantAccount;
      }
      fun(param).then(res => {
        this.$notify({
          title: '提示',
          message: text,
          type: 'success',
          duration: 5000
        });
        this.aidform = new InitParam();
        this.toQuery();
        this.dialogVisible = false;
      });
    },
    // 取消弹框
    aidformClose() {
      this.dialogVisible = false;
      this.aidform = new InitParam();
    },
    // 通过中台id查询医生
    queryDoctorDetail() {
      queryDoctorInfo(this.user.id).then(res => {
        this.doctorInfo = res;
        console.log('医生详情-----', this.doctorInfo);
      });
    },
    // 移动电话change
    mobileNumChange(mobileNum) {
      const tempStr = '@' + this.doctorInfo.id;
      this.aidform.assistantAccount = mobileNum + tempStr;
    },
    // 根据诊所id获取当前诊所
    getClinic() {
      queryClinic('LM1101AA').then(res => {
        console.log('诊所信息', res);
        this.clinicInfo = res;
      });
    }
  }
};
</script>
<style scoped lang='scss'>
.nav-search {
  padding: 10px 0;
  .el-input {
    width: 320px;
    margin-right: 10px;
  }
  .el-date-editor--date {
    width: 150px;
  }
}
.tableHandle {
  float: right;
  margin: 0;
  padding: 0;
  margin-top: 4px;
}
.searchForm {
  width: 49%;
  display: inline-block;
  margin: 5px 0px;
}
.dialog {
  .dialog-title {
    color: #fff;
  }
}
</style>
